#ifndef LOCALCHATSERVER_H
#define LOCALCHATSERVER_H

#include <QLocalServer>
#include <QLocalSocket>
#include <QList>
#include <QString>

class LocalChatServer : public QLocalServer
{
    Q_OBJECT
    
public:
    explicit LocalChatServer(QObject *parent = nullptr);
    ~LocalChatServer();
    
    bool startServer(const QString &serverName = "LocalChatServer");
    void stopServer();
    static bool isServerRunning(const QString &serverName = "LocalChatServer");
    
signals:
    void newClientConnected(const QString &clientName);
    void clientDisconnected(const QString &clientName);
    void messageReceived(const QString &username, const QString &message);
    
private slots:
    void onNewConnection();
    void onReadyRead();
    void onDisconnected();
    
private:
    QList<QLocalSocket*> m_clients; // 客户端连接列表
    QList<QString> m_clientNames;   // 客户端用户名列表
    
    void broadcastMessage(const QString &message, QLocalSocket *exclude = nullptr);
    int getClientIndex(QLocalSocket *socket);
};

#endif // LOCALCHATSERVER_H